//
//  MXHotKeyManager.m
//  Snip
//
//  Created by EliasAbel on 2016/11/21.
//  Copyright © 2016年 EliasAbel. All rights reserved.
//

#import "MXHotKeyManager.h"
#import <Carbon/Carbon.h>
#import "NSUserNotification+Quick.h"
#import "MXLauncher.h"

OSStatus MXHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData) {
    [MXLauncher launchiChat];
    return noErr;
}

@implementation MXHotKeyManager

+ (instancetype)defaultManager {
    static MXHotKeyManager *instance;
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        instance = [[MXHotKeyManager alloc] init];
    });
    return instance;
}

- (void)registerKey {
    EventHotKeyRef       gMyHotKeyRef;
    EventHotKeyID        gMyHotKeyID;
    EventTypeSpec        eventType;
    
    eventType.eventClass = kEventClassKeyboard;
    eventType.eventKind = kEventHotKeyPressed;
    
    InstallApplicationEventHandler(&MXHotKeyHandler, 1, &eventType, NULL, NULL);
    
    gMyHotKeyID.signature = 'oick';
    gMyHotKeyID.id = 1;
    
    // cmd + shift + x 
    if (RegisterEventHotKey(kVK_ANSI_X, cmdKey + controlKey, gMyHotKeyID, GetApplicationEventTarget(), 0, &gMyHotKeyRef) != noErr) {
        [NSUserNotification notification:NSLocalizedString(@"FailedToRegisterHotKey", nil) message:NSLocalizedString(@"FailedToRegisterHotKey", nil) image:nil sound:YES identifier:nil];
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self registerKey];
    }
    return self;
}

- (void)foo {
    
}

@end
